<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - running_gradient.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2016  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_RuNNING_GRADIENT_Hh_
<font color='#0000FF'>#define</font> DLIB_RuNNING_GRADIENT_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='running_gradient_abstract.h.html'>running_gradient_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../serialize.h.html'>../serialize.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cmath<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>algorithm<font color='#5555FF'>&gt;</font>


<font color='#0000FF'>namespace</font> dlib
<b>{</b>
    <font color='#0000FF'>class</font> <b><a name='running_gradient'></a>running_gradient</b> 
    <b>{</b>
    <font color='#0000FF'>public</font>:

        <b><a name='running_gradient'></a>running_gradient</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b><font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>
        <b>{</b>
            n <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            R <font color='#5555FF'>=</font> identity_matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font><font color='#979000'>1e6</font>;
            w <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            residual_squared <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> <b><a name='current_n'></a>current_n</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#0000FF'>return</font> n;
        <b>}</b>

        <font color='#0000FF'><u>void</u></font> <b><a name='add'></a>add</b><font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>double</u></font> y
        <font face='Lucida Console'>)</font>
        <b>{</b>
            matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>2</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> x;
            x <font color='#5555FF'>=</font> n, <font color='#979000'>1</font>;

            <font color='#009900'>// Do recursive least squares computations
</font>            <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> temp <font color='#5555FF'>=</font> <font color='#979000'>1</font> <font color='#5555FF'>+</font> <font color='#BB00BB'>trans</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font color='#5555FF'>*</font>R<font color='#5555FF'>*</font>x;
            matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>2</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> tmp <font color='#5555FF'>=</font> R<font color='#5555FF'>*</font>x;
            R <font color='#5555FF'>=</font> R <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font>tmp<font color='#5555FF'>*</font><font color='#BB00BB'>trans</font><font face='Lucida Console'>(</font>tmp<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>/</font>temp;
            <font color='#009900'>// R should always be symmetric.  This line improves numeric stability of this algorithm.
</font>            R <font color='#5555FF'>=</font> <font color='#979000'>0.5</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font>R <font color='#5555FF'>+</font> <font color='#BB00BB'>trans</font><font face='Lucida Console'>(</font>R<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            w <font color='#5555FF'>=</font> w <font color='#5555FF'>+</font> R<font color='#5555FF'>*</font>x<font color='#5555FF'>*</font><font face='Lucida Console'>(</font>y <font color='#5555FF'>-</font> <font color='#BB00BB'>trans</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font color='#5555FF'>*</font>w<font face='Lucida Console'>)</font>;

            <font color='#009900'>// Also, recursively keep track of the residual error between the given value
</font>            <font color='#009900'>// and what our linear predictor outputs.
</font>            residual_squared <font color='#5555FF'>=</font> residual_squared <font color='#5555FF'>+</font> std::<font color='#BB00BB'>pow</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>y <font color='#5555FF'>-</font> <font color='#BB00BB'>trans</font><font face='Lucida Console'>(</font>x<font face='Lucida Console'>)</font><font color='#5555FF'>*</font>w<font face='Lucida Console'>)</font>,<font color='#979000'>2.0</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font>temp;

            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>n;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> <b><a name='gradient'></a>gradient</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>1</font>,
                "<font color='#CC0000'>\t double running_gradient::gradient()</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You must add more values into this object before calling this function.</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>return</font> <font color='#BB00BB'>w</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> <b><a name='intercept'></a>intercept</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font>,
                "<font color='#CC0000'>\t double running_gradient::intercept()</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You must add more values into this object before calling this function.</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>return</font> <font color='#BB00BB'>w</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'><u>double</u></font> <b><a name='standard_error'></a>standard_error</b> <font face='Lucida Console'>(</font> 
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font>,
                "<font color='#CC0000'>\t double running_gradient::standard_error()</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You must add more values into this object before calling this function.</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font face='Lucida Console'>)</font>;


            <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> s <font color='#5555FF'>=</font> residual_squared<font color='#5555FF'>/</font><font face='Lucida Console'>(</font>n<font color='#5555FF'>-</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> adjust <font color='#5555FF'>=</font> <font color='#979000'>12.0</font><font color='#5555FF'>/</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>pow</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,<font color='#979000'>3.0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>-</font> <font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> std::<font color='#BB00BB'>sqrt</font><font face='Lucida Console'>(</font>s<font color='#5555FF'>*</font>adjust<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> <b><a name='probability_gradient_less_than'></a>probability_gradient_less_than</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>double</u></font> thresh
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font>,
                "<font color='#CC0000'>\t double running_gradient::probability_gradient_less_than()</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You must add more values into this object before calling this function.</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>return</font> <font color='#BB00BB'>normal_cfd</font><font face='Lucida Console'>(</font>thresh, <font color='#BB00BB'>gradient</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>standard_error</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> <b><a name='probability_gradient_greater_than'></a>probability_gradient_greater_than</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>double</u></font> thresh
        <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
        <b>{</b>
            <font color='#009900'>// make sure requires clause is not broken
</font>            <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font>,
                "<font color='#CC0000'>\t double running_gradient::probability_gradient_greater_than()</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You must add more values into this object before calling this function.</font>"
                <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t this: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
                <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>return</font> <font color='#979000'>1</font><font color='#5555FF'>-</font><font color='#BB00BB'>probability_gradient_less_than</font><font face='Lucida Console'>(</font>thresh<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'>friend</font> <font color='#0000FF'><u>void</u></font> <b><a name='serialize'></a>serialize</b> <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> running_gradient<font color='#5555FF'>&amp;</font> item, std::ostream<font color='#5555FF'>&amp;</font> out<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>int</u></font> version <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>version, out<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.n, out<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.R, out<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.w, out<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.residual_squared, out<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'>friend</font> <font color='#0000FF'><u>void</u></font> <b><a name='deserialize'></a>deserialize</b> <font face='Lucida Console'>(</font>running_gradient<font color='#5555FF'>&amp;</font> item, std::istream<font color='#5555FF'>&amp;</font> in<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>int</u></font> version <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>version, in<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>version <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
                <font color='#0000FF'>throw</font> <font color='#BB00BB'>serialization_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unexpected version found while deserializing dlib::running_gradient.</font>"<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.n, in<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.R, in<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.w, in<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.residual_squared, in<font face='Lucida Console'>)</font>;
        <b>}</b>

    <font color='#0000FF'>private</font>:

        <font color='#0000FF'>static</font> <font color='#0000FF'><u>double</u></font> <b><a name='normal_cfd'></a>normal_cfd</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> value, <font color='#0000FF'><u>double</u></font> mean, <font color='#0000FF'><u>double</u></font> stddev<font face='Lucida Console'>)</font> 
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>stddev <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>value <font color='#5555FF'>&lt;</font> mean<font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>value <font color='#5555FF'>&gt;</font> mean<font face='Lucida Console'>)</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>1</font>;
                <font color='#0000FF'>else</font>
                    <font color='#0000FF'>return</font> <font color='#979000'>0.5</font>;
            <b>}</b>
            value <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>value<font color='#5555FF'>-</font>mean<font face='Lucida Console'>)</font><font color='#5555FF'>/</font>stddev;
            <font color='#0000FF'>return</font> <font color='#979000'>0.5</font> <font color='#5555FF'>*</font> <font color='#BB00BB'>erfc</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font>value <font color='#5555FF'>/</font> std::<font color='#BB00BB'>sqrt</font><font face='Lucida Console'>(</font><font color='#979000'>2.0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'><u>double</u></font> n;
        matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>2</font>,<font color='#979000'>2</font><font color='#5555FF'>&gt;</font> R;
        matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>2</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> w;
        <font color='#0000FF'><u>double</u></font> residual_squared;
    <b>}</b>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>double</u></font> <b><a name='probability_gradient_less_than'></a>probability_gradient_less_than</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container,
        <font color='#0000FF'><u>double</u></font> thresh
    <font face='Lucida Console'>)</font>
    <b>{</b>
        running_gradient g;
        <font color='#0000FF'>for</font><font face='Lucida Console'>(</font><font color='#0000FF'>auto</font><font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> v : container<font face='Lucida Console'>)</font>
            g.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>;

        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>g.<font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font>,
            "<font color='#CC0000'>\t double probability_gradient_less_than()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You need more than 2 elements in the given container to call this function.</font>"
        <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> g.<font color='#BB00BB'>probability_gradient_less_than</font><font face='Lucida Console'>(</font>thresh<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>double</u></font> <b><a name='probability_gradient_greater_than'></a>probability_gradient_greater_than</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container,
        <font color='#0000FF'><u>double</u></font> thresh
    <font face='Lucida Console'>)</font>
    <b>{</b>
        running_gradient g;
        <font color='#0000FF'>for</font><font face='Lucida Console'>(</font><font color='#0000FF'>auto</font><font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> v : container<font face='Lucida Console'>)</font>
            g.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>;

        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>g.<font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font>,
            "<font color='#CC0000'>\t double probability_gradient_greater_than()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t You need more than 2 elements in the given container to call this function.</font>"
        <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> g.<font color='#BB00BB'>probability_gradient_greater_than</font><font face='Lucida Console'>(</font>thresh<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font> 
    <font color='#0000FF'><u>double</u></font> <b><a name='find_upper_quantile'></a>find_upper_quantile</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container_,
        <font color='#0000FF'><u>double</u></font> quantile
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> quantile <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> quantile <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1.0</font><font face='Lucida Console'>)</font>;

        <font color='#009900'>// copy container into a std::vector
</font>        std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>container</font><font face='Lucida Console'>(</font>container_.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, container_.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>container.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'><u>size_t</u></font> idx_upper <font color='#5555FF'>=</font> std::<font color='#BB00BB'>round</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>container.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font color='#5555FF'>-</font>quantile<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        std::<font color='#BB00BB'>nth_element</font><font face='Lucida Console'>(</font>container.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, container.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font>idx_upper, container.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>auto</font> upper_q <font color='#5555FF'>=</font> <font color='#5555FF'>*</font><font face='Lucida Console'>(</font>container.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font>idx_upper<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> upper_q;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font> 
    <font color='#0000FF'><u>size_t</u></font> <b><a name='count_steps_without_decrease'></a>count_steps_without_decrease</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container,
        <font color='#0000FF'><u>double</u></font> probability_of_decrease <font color='#5555FF'>=</font> <font color='#979000'>0.51</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0.5</font> <font color='#5555FF'>&lt;</font> probability_of_decrease <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> probability_of_decrease <font color='#5555FF'>&lt;</font> <font color='#979000'>1</font>,
            "<font color='#CC0000'>\t size_t count_steps_without_decrease()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t probability_of_decrease: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> probability_of_decrease 
        <font face='Lucida Console'>)</font>;

        running_gradient g;
        <font color='#0000FF'><u>size_t</u></font> count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'><u>size_t</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>auto</font> i <font color='#5555FF'>=</font> container.<font color='#BB00BB'>rbegin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; i <font color='#5555FF'>!</font><font color='#5555FF'>=</font> container.<font color='#BB00BB'>rend</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j;
            g.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>i<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>g.<font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// Note that this only looks backwards because we are looping over the
</font>                <font color='#009900'>// container backwards.  So here we are really checking if the gradient isn't
</font>                <font color='#009900'>// decreasing.
</font>                <font color='#0000FF'><u>double</u></font> prob_decreasing <font color='#5555FF'>=</font> g.<font color='#BB00BB'>probability_gradient_greater_than</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#009900'>// If we aren't confident things are decreasing.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>prob_decreasing <font color='#5555FF'>&lt;</font> probability_of_decrease<font face='Lucida Console'>)</font>
                    count <font color='#5555FF'>=</font> j;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>return</font> count;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font> 
    <font color='#0000FF'><u>size_t</u></font> <b><a name='count_steps_without_decrease_robust'></a>count_steps_without_decrease_robust</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container,
        <font color='#0000FF'><u>double</u></font> probability_of_decrease <font color='#5555FF'>=</font> <font color='#979000'>0.51</font>,
        <font color='#0000FF'><u>double</u></font> quantile_discard <font color='#5555FF'>=</font> <font color='#979000'>0.10</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> quantile_discard <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> quantile_discard <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0.5</font> <font color='#5555FF'>&lt;</font> probability_of_decrease <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> probability_of_decrease <font color='#5555FF'>&lt;</font> <font color='#979000'>1</font>,
            "<font color='#CC0000'>\t size_t count_steps_without_decrease_robust()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t probability_of_decrease: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> probability_of_decrease 
        <font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>container.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

        <font color='#0000FF'>const</font> <font color='#0000FF'>auto</font> quantile_thresh <font color='#5555FF'>=</font> <font color='#BB00BB'>find_upper_quantile</font><font face='Lucida Console'>(</font>container, quantile_discard<font face='Lucida Console'>)</font>; 

        running_gradient g;
        <font color='#0000FF'><u>size_t</u></font> count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'><u>size_t</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>auto</font> i <font color='#5555FF'>=</font> container.<font color='#BB00BB'>rbegin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; i <font color='#5555FF'>!</font><font color='#5555FF'>=</font> container.<font color='#BB00BB'>rend</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j;
            <font color='#009900'>// ignore values that are too large
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>*</font>i <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> quantile_thresh<font face='Lucida Console'>)</font>
                g.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>i<font face='Lucida Console'>)</font>;

            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>g.<font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// Note that this only looks backwards because we are looping over the
</font>                <font color='#009900'>// container backwards.  So here we are really checking if the gradient isn't
</font>                <font color='#009900'>// decreasing.
</font>                <font color='#0000FF'><u>double</u></font> prob_decreasing <font color='#5555FF'>=</font> g.<font color='#BB00BB'>probability_gradient_greater_than</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#009900'>// If we aren't confident things are decreasing.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>prob_decreasing <font color='#5555FF'>&lt;</font> probability_of_decrease<font face='Lucida Console'>)</font>
                    count <font color='#5555FF'>=</font> j;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>return</font> count;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T
        <font color='#5555FF'>&gt;</font> 
    <font color='#0000FF'><u>size_t</u></font> <b><a name='count_steps_without_increase'></a>count_steps_without_increase</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> container,
        <font color='#0000FF'><u>double</u></font> probability_of_increase <font color='#5555FF'>=</font> <font color='#979000'>0.51</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0.5</font> <font color='#5555FF'>&lt;</font> probability_of_increase <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> probability_of_increase <font color='#5555FF'>&lt;</font> <font color='#979000'>1</font>,
            "<font color='#CC0000'>\t size_t count_steps_without_increase()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t probability_of_increase: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> probability_of_increase 
        <font face='Lucida Console'>)</font>;

        running_gradient g;
        <font color='#0000FF'><u>size_t</u></font> count <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'><u>size_t</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>auto</font> i <font color='#5555FF'>=</font> container.<font color='#BB00BB'>rbegin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; i <font color='#5555FF'>!</font><font color='#5555FF'>=</font> container.<font color='#BB00BB'>rend</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j;
            g.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>i<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>g.<font color='#BB00BB'>current_n</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>2</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// Note that this only looks backwards because we are looping over the
</font>                <font color='#009900'>// container backwards.  So here we are really checking if the gradient isn't
</font>                <font color='#009900'>// increasing.
</font>                <font color='#0000FF'><u>double</u></font> prob_increasing <font color='#5555FF'>=</font> g.<font color='#BB00BB'>probability_gradient_less_than</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#009900'>// If we aren't confident things are increasing.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>prob_increasing <font color='#5555FF'>&lt;</font> probability_of_increase<font face='Lucida Console'>)</font>
                    count <font color='#5555FF'>=</font> j;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>return</font> count;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_RuNNING_GRADIENT_Hh_
</font>


</pre></body></html>